package com.ph.elec.gwt.register.client.ui;

import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.ph.elec.gwt.register.client.OptionLists;
import com.ph.elec.gwt.util.UIUtils;
import com.ph.elec.gwt.util.UIUtils.StdDecoratorPanel;

public class VolunteerHelpPanel extends StdDecoratorPanel {
	
	public RadioButton rdoVolunteer = new RadioButton("volunteer", "I would like to volunteer.");
	public RadioButton rdoNotVolunteer = new RadioButton("volunteer", "I'll be a spectator to the portal.");

	public CheckBox chkVTime = new CheckBox("I'll volunteer my time.");
	public CheckBox chkVCash = new CheckBox("I'll give a donation for a Free Elections.");

	VerticalPanel vtpVolunteer = new VerticalPanel();
	VerticalPanel vtpVTime = new VerticalPanel();
	VerticalPanel vtpVITTime = new VerticalPanel();
	VerticalPanel vtpVCash = new VerticalPanel();

	public ListBox lstVPart = new ListBox();
	public ListBox lstVITPart = new ListBox();

	public TextBox txtCashAmount = new TextBox();
	
	public VolunteerHelpPanel() {
		rdoVolunteer.setValue(true);
		vtpRoot.add(rdoVolunteer);
		
		// Volunteer Items
		vtpVolunteer.add(chkVTime);
		
		vtpVTime.add(UIUtils.decorateFieldName(
				UIUtils.decorateWidth(
						UIUtils.decorateListBox(lstVPart, OptionLists.VOLUNTEER_PART), 
						"350px"), 
				"Preferred Volunteer Assignment", 
				true));
		vtpVITTime.add(UIUtils.decorateFieldName(
				UIUtils.decorateWidth(
						UIUtils.decorateListBox(lstVITPart, OptionLists.VOLUNTEER_IT_PART), 
						"250px"), 
				"IT Committee", 
				true));
		vtpVITTime.setVisible(false);
		vtpVTime.add(vtpVITTime);
		vtpVTime.addStyleName("L2Indent");
		vtpVTime.setVisible(false);
		
		vtpVolunteer.add(vtpVTime);
		
		vtpVolunteer.add(chkVCash);

		vtpVCash.add(UIUtils.decorateFieldName(
				UIUtils.decorateTextBox(txtCashAmount, "150px", 20, TextBox.ALIGN_RIGHT), 
				"Cash Amount in Pesos  (No cents)", 
				true));
		vtpVCash.addStyleName("L2Indent");
		vtpVCash.setVisible(false);
		vtpVolunteer.addStyleName("L1Indent");
		vtpVolunteer.add(vtpVCash);
		
		vtpRoot.add(vtpVolunteer);
		vtpRoot.add(rdoNotVolunteer);

		add(vtpRoot);
		setWidth("500px");
		
		initializeContents();
		initializeHandlers();
	}
	
	private void initializeContents() {
	}

	protected void initializeHandlers() {
		// Show/Hide Volunteer Part
		rdoNotVolunteer.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				vtpVolunteer.setVisible(false);
			}
		});
		rdoVolunteer.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				vtpVolunteer.setVisible(true);
			}
		});
		chkVCash.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				if (chkVCash.getValue()) 
					vtpVCash.setVisible(true);
				else
					vtpVCash.setVisible(false);
			}
		});
		chkVTime.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				if (chkVTime.getValue()) 
					vtpVTime.setVisible(true);
				else
					vtpVTime.setVisible(false);
			}
		});
		lstVPart.addChangeHandler(new ChangeHandler() {
			public void onChange(ChangeEvent event) {
				if (lstVPart.getSelectedIndex() == 12) 
					vtpVITTime.setVisible(true);
				else
					vtpVITTime.setVisible(false);
			}
		});
	}

	public boolean validate() {
		boolean invalid = false;
		vtpErrorMessage.clear();
		if (rdoVolunteer.getValue()) {
			if (!chkVCash.getValue() && !chkVTime.getValue()) {
				addErrorMessage("Please select one aspect of Volunteering.");
				invalid = true;
			}
			if (chkVTime.getValue() && lstVPart.getSelectedIndex() == 0) {
				addErrorMessage("Please select a Preffered Volunteer Assignment.");
				invalid = true;
			} else if (chkVTime.getValue() && lstVPart.getSelectedIndex() == 12 && lstVITPart.getSelectedIndex() == 0) {
				addErrorMessage("Please select a IT Committee.");
				invalid = true;
			}
			if (chkVCash.getValue() && txtCashAmount.getText().trim().length() > 0) {
				try {
					Integer.parseInt(txtCashAmount.getText().trim());
				} catch (NumberFormatException e) {
					addErrorMessage("Please enter a valid amount.  Do not include cents and only numbers.");
					invalid = true;
				}
			} else if (chkVCash.getValue()) {
				addErrorMessage("Please input an amount you would like to donate.");
				invalid = true;
			}
		}
		if (invalid) {
			return false;
		}
		return true;
	}
}
